package com.tanhua.listener;

import cn.hutool.core.date.DateUtil;
import com.tanhua.domain.Log;
import com.tanhua.mapper.LogMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Date;
import java.util.Map;

@RocketMQMessageListener(topic = "tanhua-log",consumerGroup = "tanhua-log-group")
@Component
public class LogMessageListener implements RocketMQListener<Map> {
    @Autowired
    private LogMapper logMapper;
    @Override//只要监听到队列中有数据 该方法就执行 Map ：队列中的数据
    public void onMessage(Map map) {
        System.out.println("我在监听tanhua-log 我执行....");

        // 创建一个表对应的实体 封装属性 做插入日志表数据
        String  userId = map.get("userId").toString();
        long userid2 = Long.parseLong(userId);
        String type = (String)map.get("type");
        Log log = new Log();
        log.setUserId(userid2);
        log.setType(type);
        String time = DateUtil.format(new Date(), "yyyy-MM-dd");
        log.setLogTime(time);

        logMapper.insert(log);
    }
}
